<template>
  <div class="flex_cm container" style="height: 100%">
    <search
      :data="header_list"
      @changeIsShow="
        (e) => {
          header_list[e].isShow = !header_list[e].isShow
          header_list[e].value = ''
        }
      "
      @search="search_key"
      @reset="
        header_list.forEach((item) => {
          item.value = ''
        }),
          search_key()
      "
    ></search>
    <div class="p_0_10 bg_fff flex1">
      <div class="flex_js p_10_0">
        <div class="c_666 fw" v-if="add_form_data.type == '1'">
          模板类型管理
        </div>
        <div class="c_666 fw" v-if="add_form_data.type == '2'">
          配件类型管理
        </div>
        <div class="flex">
          <div v-if="add_form_data.type == '1'">
            <el-button
              type="primary"
              @click="mould_tabs_box = true"
              v-if="role_list[6].has_role"
              >顶部栏/表格</el-button
            >
            <el-button
              type="primary"
              @click="metadata_box = true"
              v-if="role_list[7].has_role"
              >元数据</el-button
            >
            <span class="c_999 m_0_20">|</span>
          </div>

          <el-button
            type="primary"
            @click="show_box = true"
            v-if="role_list[3].has_role"
            >新增</el-button
          >

          <setTable
            :data="header_list"
            :height_size="height_size"
            @changeShowTable="
              (e) => {
                header_list[e].showTable = !header_list[e].showTable
              }
            "
            @resetTableSet="
              () => {
                header_list.forEach((item) => {
                  item.showTable = true
                })
              }
            "
            @changeHeightSize="
              (e) => {
                height_size = e
              }
            "
          ></setTable>
        </div>
      </div>
      <el-table
        style="text-align: center"
        :data="list"
        :max-height="max_height"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        border
        highlight-current-row
        v-loading="loading"
        :default-sort="{ prop: 'sort', order: 'ascending' }"
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column
          label="类型代号"
          sortable
          prop="code"
          key="类型代号"
          v-if="header_list[0].showTable"
        ></el-table-column>
        <el-table-column
          label="类型名称"
          sortable
          prop="name"
          key="类型名称"
          v-if="header_list[1].showTable"
        ></el-table-column>
        <el-table-column
          label="排序"
          sortable
          prop="sort"
          key="排序"
          v-if="header_list[2].showTable"
        ></el-table-column>
        <el-table-column
          label="特殊开放企业"
          sortable
          key="类型名称"
          prop="enterpriseList"
          v-if="header_list[3].showTable"
        >
          <template #default="scope: any">
            <div v-for="item in scope.row.enterpriseList" :key="item.id">
              {{ item?.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          sortable
          prop="isEnable"
          key="状态"
          v-if="header_list[4].showTable"
        >
          <template #default="scope: any">
            <div v-show="scope.row?.isEnable" style="color: #3580eb">
              已启用
            </div>
            <div v-show="!scope.row.isEnable">已停用</div>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          key="备注"
          v-if="header_list[5].showTable"
          ><template #default="scope: any"
            ><div class="text_over">
              <el-tooltip :content="scope.row.remark" placement="bottom">
                {{ scope.row.remark }}
              </el-tooltip>
            </div></template
          ></el-table-column
        >
        <el-table-column label="操作" width="174px">
          <template #default="scope: any">
            <div class="flex_cc" style="column-gap: 20px">
              <el-tooltip
                v-show="(role_list[0] as any).path.indexOf('status') != -1 && (role_list[0] as any).has_role"
                :content="scope.row.isEnable ? '停用' : '启用'"
                placement="bottom"
              >
                <svgBtn
                  :icon="
                    scope.row.isEnable
                      ? 'table_status_disable'
                      : 'table_status_enable'
                  "
                  style="font-size: 20px"
                  color="#45474A"
                  @click="
                    delete_list({
                      item: scope.row,
                      type: scope.row.isEnable ? 2 : 1,
                    })
                  "
                ></svgBtn>
              </el-tooltip>
              <el-tooltip
                v-show="role_list[1] && (role_list[1] as any).has_role"
                content="编辑"
                placement="bottom"
              >
                <svgBtn
                  icon="table_edit"
                  style="font-size: 20px"
                  @click="update_list(scope)"
                ></svgBtn>
              </el-tooltip>
              <el-tooltip
                v-show="role_list[4] && (role_list[4] as any).has_role && scope.row.parentId != -1 && add_form_data.type == '1'"
                content="配置"
                placement="bottom"
              >
                <svgBtn
                  icon="setup"
                  color="#333"
                  style="font-size: 20px"
                  @click="setUp_list(scope)"
                  v-show="scope.row?.parentId != -1"
                ></svgBtn>
              </el-tooltip>
              <el-tooltip
                v-show="role_list[2] && (role_list[2] as any).has_role && (scope.row.parentId != -1 ||
                (scope.row.parentId == -1 && scope.row?.children?.length == 0))"
                content="删除"
                placement="bottom"
              >
                <svgBtn
                  icon="table_delete"
                  style="font-size: 20px"
                  @click="
                    delete_list({ item: scope.row, type: 0, delete_tips: 1 })
                  "
                ></svgBtn>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <el-drawer
    v-model="show_box"
    direction="rtl"
    size="50%"
    @close="resetForm(mouldFormData)"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4 v-if="add_form_data.id == ''">新增模板类型</h4>
      <h4 v-else>编辑模板类型</h4>
    </template>
    <template #default>
      <el-form
        :model="add_form_data"
        :rules="rules"
        ref="mouldFormData"
        label-width="100px"
      >
        <el-form-item label="类型代号" prop="code">
          <el-input
            v-model="add_form_data.code"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="类型名称" prop="name">
          <el-input
            v-model="add_form_data.name"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="父级类型" v-if="add_form_data.parentId != '-1'">
          <el-select v-model="add_form_data.parentId">
            <el-option
              v-for="item in list"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型排序" prop="sort">
          <el-input
            v-model="add_form_data.sort"
            placeholder="请输入"
            show-word-limit
            maxlength="4"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            type="textarea"
            resize="none"
            :autosize="{ minRows: 2, maxRows: 6 }"
            v-model="add_form_data.remark"
            placeholder="请输入"
            show-word-limit
            maxlength="300"
          ></el-input>
        </el-form-item>
        <el-form-item label="特殊开放企业">
          <el-select
            v-model="add_form_data.enterpriseList"
            multiple
            reserve-keyword
            filterable
            :remote-method="remoteMethod"
            @blur="
              enterprise_list.forEach((item) => {
                item.disabled = false
              })
            "
          >
            <div v-for="item in enterprise_list" :key="item.id">
              <el-option
                v-if="!item.disabled"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </div>
          </el-select>
        </el-form-item>
        <div class="m_top10 tr">
          <el-button @click="resetForm(mouldFormData)">取消</el-button>
          <el-button type="primary" @click="submit(mouldFormData)"
            >确定</el-button
          >
        </div>
      </el-form>
    </template>
  </el-drawer>
  <el-drawer
    v-model="metadata_box"
    direction="rtl"
    size="70%"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4>元数据</h4>
    </template>
    <template #default>
      <div class="flex_js m_bottom10 p_bottom10 border_bottom">
        <div class="meta_tabs">
          <div
            v-for="(item, index) in metadata_type_list"
            :key="index"
            @click=";(metadata_pageData.type_value = index), get_metadata()"
            :class="metadata_pageData.type_value == index ? 'select' : ''"
          >
            {{ item }}
          </div>
        </div>
        <el-button
          type="primary"
          @click="
            ;(metadata_form_data.sort = (metadata[metadata.length - 1].sort +
              1) as any),
              (add_metadata_box = true)
          "
          >新增</el-button
        >
      </div>
      <div class="m_bottom10 flex">
        <span>编码：</span>
        <el-input
          style="width: 200px"
          v-model="metadata_pageData.code"
          placeholder="请输入"
          clearable
        ></el-input>
        <span style="margin-left: 10px"> 名称：</span>
        <el-input
          style="width: 200px"
          v-model="metadata_pageData.name"
          placeholder="请输入"
          clearable
        ></el-input>
        <el-button @click="get_metadata" class="m_left10" type="primary"
          >搜索</el-button
        >
        <el-button
          @click="
            ;(metadata_pageData.code = ''),
              (metadata_pageData.name = ''),
              get_metadata()
          "
          class="m_left10"
          type="primary"
          >重置</el-button
        >
      </div>
      <el-table
        :data="metadata"
        style="text-align: center"
        max-height="600"
        v-loading="loading1"
        :default-sort="{ prop: 'sort', order: '' }"
        border
      >
        <el-table-column
          prop="sort"
          label="排序"
          width="80px"
        ></el-table-column>
        <el-table-column label="编码" prop="code"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="类型" prop="code">
          <template #default="scope: any">
            <div v-show="scope.row?.type == 1">文本</div>
            <div v-show="scope.row?.type == 2">数字</div>
            <div v-show="scope.row?.type == 3">单选（二选一）</div>
            <div v-show="scope.row?.type == 4">下拉单选</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope: any">
            <el-button @click="update_metadata(scope)">编辑</el-button>
            <el-button
              @click="delete_list({ item: scope.row, type: 0, delete_tips: 2 })"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="m_top10 tr">
        <el-button @click="metadata_box = false">取消</el-button>
        <el-button type="primary" @click="metadata_box = false">确定</el-button>
      </div>
    </template>
  </el-drawer>
  <el-drawer
    v-model="add_metadata_box"
    direction="rtl"
    size="50%"
    @close="resetForm(metadataFormData)"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4 v-if="metadata_form_data.id == ''">新增元数据</h4>
      <h4 v-else>编辑元数据</h4>
    </template>
    <template #default>
      <el-form
        :model="metadata_form_data"
        ref="metadataFormData"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="组合类型">
          <el-radio
            v-model="metadata_form_data.isCombination"
            @change="
              ;(metadata_form_data.code = ''), (select_metadata_code_list = [])
            "
            :label="false"
            >否</el-radio
          >
          <el-radio
            v-model="metadata_form_data.isCombination"
            @change="
              ;(metadata_form_data.code = ''), (select_metadata_code_list = [])
            "
            :label="true"
            >是</el-radio
          >
        </el-form-item>
        <el-form-item label="组合规则" v-if="metadata_form_data.isCombination">
          <el-select v-model="combination_value" @change="get_metadata_code">
            <el-option
              v-for="item in metadata_combination_list"
              :label="item.name"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="编码所需元数据"
          v-if="metadata_form_data.isCombination"
        >
          <el-select
            v-model="select_metadata_code_list"
            @change="select_metadata_code"
            filterable
            multiple
            style="width: 100%"
          >
            <el-option
              v-for="item in metadata_select_list"
              :key="item.name"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="所需元数据数量"
          v-if="
            select_metadata_code_list.length &&
            combination_value == 'sum' &&
            metadata_form_data.isCombination
          "
        >
          <div class="flex">
            <el-input
              class="m_right10"
              type="number"
              v-for="(item, index) in select_metadata_code_list_num"
              v-model="item.num"
              @input="get_select_metadata_code_list_num(index)"
              placeholder="请输入"
              show-word-limit
              maxlength="4"
            ></el-input>
          </div>
        </el-form-item>
        <el-form-item label="元数据编码" prop="code">
          <el-input
            :disabled="metadata_form_data.isCombination"
            v-model="metadata_form_data.code"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>

        <el-form-item label="元数据名称" prop="name">
          <el-input
            v-model="metadata_form_data.name"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="元数据排序">
          <el-input
            v-model="metadata_form_data.sort"
            placeholder="请输入"
            show-word-limit
            maxlength="4"
          ></el-input>
        </el-form-item>
        <el-form-item label="元数据类型" prop="type">
          <el-select v-model="metadata_form_data.type">
            <el-option
              v-for="item in type_list"
              :key="item.value"
              :value="item.value + ''"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型可选值" v-if="metadata_form_data.type === '4'">
          <div>
            <div
              v-for="(item, index) in type_list_option"
              :key="item"
              class="flex"
            >
              <div>{{ item.code }} -- {{ item.name }}</div>

              <el-button
                @click="
                  type_list_option.splice(index, 1),
                    (type_list_option_item.sort -= 1)
                "
                class="m_left10"
                >删除</el-button
              >
            </div>
            <div class="flex">
              <div class="flex">
                编码：
                <el-input
                  v-if="!metadata_form_data.isCombination"
                  style="width: 200px; margin-right: 10px"
                  v-model="type_list_option_item.code"
                  placeholder="请输入"
                  show-word-limit
                  maxlength="30"
                ></el-input>
                <div v-else class="flex m_right10">
                  <div v-for="item in type_list_option_item.code_list">
                    <el-select
                      v-if="item.type == 4"
                      v-model="item.code"
                      style="width: 60px"
                    >
                      <el-option
                        v-for="item1 in item.metadataValueList"
                        :value="item1.code"
                        :label="item1.name"
                      ></el-option>
                    </el-select>
                    <el-input
                      v-else
                      v-model="item.code"
                      @input="
                        (val) => {
                          ;/^(0|[1-9][0-9]*)$/.test(val)
                            ? (item.code = val)
                            : (item.code = '')
                        }
                      "
                      type="number"
                      style="width: 40px"
                      show-word-limit
                      maxlength="30"
                    ></el-input>
                  </div>
                </div>
                名称：
                <el-input
                  style="width: 200px"
                  v-model="type_list_option_item.name"
                  placeholder="请输入"
                  show-word-limit
                  maxlength="30"
                ></el-input>
              </div>
              <el-button class="m_left10" @click="add_type_list"
                >确定</el-button
              >
            </div>
          </div>
        </el-form-item>
        <el-form-item label="元数据备注">
          <el-input
            type="textarea"
            resize="none"
            :autosize="{ minRows: 2, maxRows: 6 }"
            v-model="metadata_form_data.remark"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="适用范围">
          <el-checkbox v-model="metadata_form_data.isTem"
            >模板元数据</el-checkbox
          >
          <el-checkbox v-model="metadata_form_data.isAttribute"
            >模板属性元数据</el-checkbox
          >
          <el-checkbox v-model="metadata_form_data.isRule"
            >模板条件元数据</el-checkbox
          >
          <el-checkbox v-model="metadata_form_data.isPartsAttribute"
            >配件属性元数据</el-checkbox
          >
          <el-checkbox v-model="metadata_form_data.isPartsRule"
            >配件条件元数据</el-checkbox
          >
          <el-checkbox v-model="metadata_form_data.isMyPartsAttribute"
            >配件自身属性元数据</el-checkbox
          >
          <el-checkbox v-model="metadata_form_data.isMyPartsRule"
            >配件自身条件元数据</el-checkbox
          >
        </el-form-item>
        <el-form-item
          label="判断条件"
          v-if="
            metadata_form_data.isRule ||
            metadata_form_data.isPartsRule ||
            metadata_form_data.isMyPartsRule
          "
        >
          <el-select v-model="metadata_form_data.judgmentType">
            <el-option
              v-for="item in dict_list_1"
              :label="item.dictValue"
              :value="parseInt(item.dictCode)"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="m_top10 tr">
          <el-button @click="resetForm(metadataFormData)">取消</el-button>
          <el-button type="primary" @click="submit_metadata(metadataFormData)"
            >确定</el-button
          >
        </div>
      </el-form>
    </template>
  </el-drawer>

  <el-drawer
    v-model="mould_tabs_box"
    direction="rtl"
    size="50%"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4>顶部栏/表格</h4>
    </template>
    <template #default>
      <el-button
        type="primary"
        class="m_bottom10"
        @click="add_mould_tabs_box = true"
        >新增</el-button
      >
      <el-table
        :data="mould_tabs"
        style="text-align: center"
        max-height="600"
        v-loading="loading2"
        row-key="id"
        border
        highlight-current-row
        :row-style="{
          height: height_size == 2 ? '50px' : '',
        }"
        :cell-style="{
          padding: height_size == 3 ? '0' : '',
        }"
        :default-sort="{ prop: 'name', order: 'descending' }"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column label="编码" prop="code"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope: any">
            <el-button @click="update_mould_tabs(scope)">编辑</el-button>
            <el-button
              v-show="
                scope.row.parentId != -1 ||
                (scope.row.parentId == -1 && scope.row.children.length == 0)
              "
              @click="delete_list({ item: scope.row, type: 0, delete_tips: 3 })"
              >删除</el-button
            >
            <!-- <table_edit
              @edit_item="update_metadata(scope)"
              @delete_item="delete_metadata({ item: scope.row, type: 0 })"
            ></table_edit> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="m_top10 tr">
        <el-button @click="mould_tabs_box = false">取消</el-button>
        <el-button type="primary" @click="mould_tabs_box = false"
          >确定</el-button
        >
      </div>
    </template>
  </el-drawer>
  <el-drawer
    v-model="add_mould_tabs_box"
    direction="rtl"
    size="30%"
    @close="resetForm(mouldTabsFormDate)"
    :close-on-click-modal="false"
  >
    <template #title>
      <h4 v-if="add_mould_tabs_form_data.id == ''">新增顶部栏</h4>
      <h4 v-else>编辑顶部栏</h4>
    </template>
    <template #default>
      <el-form
        :model="add_mould_tabs_form_data"
        ref="mouldTabsFormDate"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="顶部栏编码">
          <el-input
            v-model="add_mould_tabs_form_data.code"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item label="顶部栏名称" prop="name">
          <el-input
            v-model="add_mould_tabs_form_data.name"
            placeholder="请输入"
            show-word-limit
            maxlength="30"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="父级类型"
          v-if="add_mould_tabs_form_data.parentId != '-1'"
        >
          <el-select v-model="add_mould_tabs_form_data.parentId">
            <el-option
              v-for="item in mould_tabs"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="顶部栏排序">
          <el-input
            v-model="add_mould_tabs_form_data.sort"
            placeholder="请输入"
            show-word-limit
            maxlength="4"
          ></el-input>
        </el-form-item>
        <el-form-item label="顶部栏备注">
          <el-input
            type="textarea"
            resize="none"
            :autosize="{ minRows: 2, maxRows: 6 }"
            v-model="add_mould_tabs_form_data.remark"
            placeholder="请输入"
            show-word-limit
            maxlength="300"
          ></el-input>
        </el-form-item>
        <div class="m_top10 tr">
          <el-button @click="resetForm(mouldTabsFormDate)">取消</el-button>
          <el-button
            type="primary"
            @click="submit_mould_tabs(mouldTabsFormDate)"
            >确定</el-button
          >
        </div>
      </el-form>
    </template>
  </el-drawer>
  <el-drawer
    v-model="show_setUp_box"
    direction="rtl"
    size="50%"
    :close-on-click-modal="false"
  >
    <template #title><h4>配置</h4></template>
    <template #default>
      <div class="flex">
        <div>选择顶部栏</div>
        <div v-for="item in mould_tabs" class="m_left20">
          <el-checkbox
            :label="item.name"
            :key="item.id"
            v-model="item.is_checked"
            @change="change_setup_tabs(item)"
          ></el-checkbox>
        </div>
      </div>
      <div
        class="p_10"
        v-if="templateFormDealList.length != 0"
        style="border: 1px solid #ccc"
      >
        <el-tabs
          v-model="templateFormDealList_index"
          type="card"
          editable
          class="demo-tabs m_top10"
          @tab-remove="remove_tab"
        >
          <el-tab-pane
            v-for="(item, index) in templateFormDealList"
            :key="index"
            :label="item.name"
            :name="index"
          >
            <div v-if="templateFormDealList_index == index">
              <div style="margin: 10px 0">
                顶部参数配置
                <el-select
                  style="width: 100%; margin: 10px 0"
                  v-model="item.templateFormMetadataDealList_1"
                  @change="(val) => selectChangeMetadata(val, index)"
                  placeholder="请选择参数"
                  filterable
                  multiple
                >
                  <el-option
                    v-for="(item2, index2) in metadata_select_list"
                    :key="index2"
                    :label="item2.name"
                    :value="item2.id"
                  />
                </el-select>
              </div>
              <div style="margin: 10px 0">
                <div class="flex_wp">
                  列表选择
                  <div
                    v-for="item2 in item.children"
                    :key="item2.id"
                    class="m_left20"
                  >
                    <el-checkbox
                      v-if="item2.parentId == item.id"
                      :label="item2.name"
                      :key="item2.id"
                      v-model="item2.is_checked"
                    ></el-checkbox>
                  </div>
                </div>
                <div
                  v-for="(item3, index3) in item.children"
                  style="margin: 20px 0 10px 0"
                >
                  <div v-if="item3.is_checked">
                    <div style="color: #3580eb">{{ item3.name }}</div>
                    <div>
                      列表参数选择<el-select
                        style="width: 100%; margin: 10px 0"
                        v-model="item3.templateFormMetadataDealList_1"
                        @change="
                          (val) =>
                            selectChangeMetadataToTable(val, index, index3)
                        "
                        placeholder="请选择列表参数"
                        multiple
                        filterable
                      >
                        <el-option
                          v-for="(item4, index4) in metadata_select_list"
                          :key="index4"
                          :label="item4.name"
                          :value="item4.id"
                        />
                      </el-select>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="tr m_top10">
        <el-button @click="show_setUp_box = false">取消</el-button>
        <el-button
          type="primary"
          @click="setup_submit"
          v-if="templateFormDealList.length != 0"
          >确定</el-button
        >
      </div>
    </template>
  </el-drawer>
  <dialog_box
    @submit_delete="submit_delete"
    @submit_edit="submit_edit"
    @close="show_delete_box = false"
    :delete_name="delete_name"
    :show_delete_box="show_delete_box"
    :delete_box_status="delete_box_status"
  ></dialog_box>
</template>

<script lang="ts" setup>
import { check_role, dictData } from '@/utils/utils' // 权限校验
import search from '@/view/components/search.vue'
import dialog_box from '@/view/components/dialog_box.vue'
import svgBtn from '@/components/svg_button.vue'
import setTable from '@/view/components/setTable.vue'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import { FormInstance, ElMessage } from 'element-plus'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import EventBus from '@/utils/EventBus'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
const store = useStore()
const route = useRoute()
const role_list = ref([
  { path: 'mould/status', has_role: false },
  { path: 'mould/edit', has_role: false },
  { path: 'mould/delete', has_role: false },
  { path: 'mould/post', has_role: false },
  { path: 'mould/setup', has_role: false },

  { path: 'mould/list', has_role: false },
  { path: 'mould_form/list', has_role: false }, // 顶部栏及其下列表权限组
  { path: 'mould_metadata/list', has_role: false }, // 元数据及其下列表权限组
])
const max_height = ref()

// type FormInstance = InstanceType<typeof ElForm>
const metadataFormData = ref<FormInstance>() // 元数据form
const mouldFormData = ref<FormInstance>() // 主列表form
const mouldTabsFormDate = ref<FormInstance>() // 顶部栏、列表form
const checkSort = (rule, value, callback) => {
  // 排序字段验证规则
  var tt = /^([1-9]\d*)$/.test(value)
  if (!value) {
    return callback(new Error('排序不能为空'))
  } else if (!tt) {
    return callback(new Error('排序必须为正整数'))
  } else {
    callback()
  }
}
const rules = ref({
  code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  sort: [{ required: true, validator: checkSort, trigger: 'blur' }],
  type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
})
const type_list = ref([
  // 元数据可选值
  { value: '1', label: '文本' },
  { value: '2', label: '数字' },
  { value: '4', label: '下拉单选' },
])
const type_list_option_item = ref({
  code: '',
  name: '',
  sort: 1,
  code_list: [],
})
const type_list_option = ref([]) // 下拉选项集合 元数据为多选框是使用
const list = ref()
const total = ref(0)
const metadata_total = ref()
const loading = ref(false)
const loading1 = ref(false)
const loading2 = ref(false)
const show_box = ref(false)
const metadata = ref() // 元数据 相关
const metadata_box = ref(false)
const add_metadata_box = ref(false)
const mould_tabs = ref() // 顶部栏 以及 下列表
const mould_tabs_box = ref(false)
const add_mould_tabs_box = ref(false)
const height_size = ref(1)
const header_list = ref([
  {
    label: '类型代号',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '类型名称',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '排序',
    value: '',
    showTable: true,
    type: 'input',
    isShow: true,
    options: [],
  },
  {
    label: '特殊开放企业',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      { value: '1', label: '是' },
      { value: '0', label: '否' },
    ],
  },
  {
    label: '状态',
    value: '',
    showTable: true,
    type: 'select',
    isShow: false,
    options: [
      { value: '1', label: '已启用' },
      { value: '0', label: '已停用' },
    ],
  },
  {
    label: '备注',
    value: '',
    showTable: true,
    type: 'input',
    isShow: false,
    options: [],
  },
])
const metadata_pageData = ref({
  // 元数据列表
  current: 1,
  pageSize: 5000,
  name: '',
  code: '',
  type_value: 0,
})
const metadata_type_list = ref([
  '全部',
  '基础元数据',
  '属性元数据',
  '条件元数据',
  '配件属性元数据',
  '配件条件元数据',
  '配件自身属性元数据',
  '配件自身条件元数据',
])
const metadata_form_data = ref({
  // 元数据表单
  id: '',
  name: '',
  code: '',
  remark: '',
  sort: '',
  type: '1',
  isCombination: false,
  isTem: true,
  isAttribute: false,
  isRule: false,
  judgmentType: 1,
  metadataValueDealList: [],
  isPartsAttribute: false,
  isPartsRule: false,
  isMyPartsRule: false,
  isMyPartsAttribute: false,
})
// 元数据组合规则列表 值
const combination_value = ref('sum')
const metadata_combination_list = ref([
  { name: '多项相加', value: 'sum' },
  { name: '包含全部', value: 'existand' },
  { name: '至少包含其一', value: 'existor' },
  { name: '全部不包含', value: 'not' },
  { name: '定值（包含其一）', value: 'in' },
])
// 元数据组合类型  所选择的元数据列表
const select_metadata_code_list = ref([])
// 根据输入的所需元数据数量  更新组合元数据编码code
const get_select_metadata_code_list_num = (e) => {
  console.log(e)
  var tt = /^([1-9]\d*)$/.test(select_metadata_code_list_num.value[e].num)
  var rr = /^-([1-9]\d*)$/.test(select_metadata_code_list_num.value[e].num)
  if (!tt && !rr) {
    select_metadata_code_list_num.value[e].num = '1'
  }
  get_metadata_code()
}
const select_metadata_code_list_num = ref([]) // 选择元数据 对应的数量数组
// 选择所需元数据 并生成编码
const select_metadata_code = (e) => {
  console.log(e)
  if (e.length) {
    metadataFormData.value.clearValidate(['code'])
  }
  // 每次更新 选择的元数据  都是重新赋值 其 对应的数量 默认1  以及该元数据类型为下拉单选时的 参数 格式 并将原有的参数 置 []
  select_metadata_code_list_num.value = []
  type_list_option_item.value.code_list = []
  type_list_option.value = []
  e.forEach((item) => {
    select_metadata_code_list_num.value.push({ num: '1' })
    metadata_select_list.value.some((item1) => {
      if (item1.code == item) {
        // 选择元数据  给对应的下拉单选类型 赋值 type == 1、2 输入框 4 下拉单选  metadataValueList 下拉 option ,code:绑定的值
        type_list_option_item.value.code_list.push({
          type: item1.type,
          metadataValueList: item1.metadataValueList,
          code: '',
        })
      }
    })
  })
  get_metadata_code()
}
// 根据选择的元数据 以及 组合规则 对应的数量 生成 自身的code
const get_metadata_code = () => {
  var str = []
  select_metadata_code_list.value.forEach((item, index) => {
    if (
      select_metadata_code_list_num.value[index].num != '1' &&
      combination_value.value == 'sum'
    ) {
      var str_1 =
        select_metadata_code_list_num.value[index].num + '*(' + item + ')'
    } else {
      var str_1 = '(' + item + ')'
    }
    str.push(str_1)
  })
  metadata_form_data.value.code =
    combination_value.value + '[' + str.join(',') + ']'
}
const add_type_list = () => {
  // 判断每一个类型编码 名称 都必填
  if (metadata_form_data.value.isCombination) {
    let str = ''
    for (let i in type_list_option_item.value.code_list) {
      if (type_list_option_item.value.code_list[i].code == '') {
        ElMessage.error('请输入每一项类型可选值的编码')
        return
      }
      // 填写编码之后 组合成 x,x,x 格式
      str +=
        type_list_option_item.value.code_list[i].code +
        ((i as any) != type_list_option_item.value.code_list.length - 1
          ? ','
          : '')
    }
    type_list_option_item.value.code = str
  }
  if (type_list_option_item.value.code == '') {
    ElMessage.error('请输入每一项类型可选值的编码')
    return
  }
  if (type_list_option_item.value.name == '') {
    ElMessage.error('请输入每一项类型可选值的名称')
    return
  }
  type_list_option.value.push(type_list_option_item.value)
  type_list_option_item.value.code_list.forEach((item) => {
    item.code = ''
  })
  type_list_option_item.value = {
    code: '',
    name: '',
    code_list: type_list_option_item.value.code_list,
    sort: type_list_option.value.length + 1,
  }
}
const add_mould_tabs_form_data = ref({
  // tab栏表单
  id: '',
  name: '',
  code: '',
  parentId: '', // 有值  则代表该项为tab栏下的列表 *****
  remark: '',
  sort: '',
})
const page_data = ref({
  current: 1,
  pageSize: 1000,
  code: '',
  name: '',
  sort: '',
  isEnable: '',
  isLimitEnterprise: '',
  remark: '',
  type: '1',
})
const add_form_data = ref({
  id: '',
  name: '',
  code: '',
  parentId: '',
  enterpriseList: [],
  isLimitEnterprise: false,
  sort: '',
  remark: '',
  isEnable: true,
  type: '1',
})

const dict_list_1 = ref()
onMounted(() => {
  if (route.name == 'mould_type') {
    // 模板
    add_form_data.value.type = '1'
    page_data.value.type = '1'
  } else if (route.name == 'parts_type') {
    // 配件
    add_form_data.value.type = '2'
    page_data.value.type = '2'
  }
  max_height.value = window.innerHeight - 270 // 带分页的 table 高度
  role_list.value.forEach((item) => {
    item.has_role = check_role(item.path)
  })
  EventBus.on('refresh', () => {
    get_list()
    get_metadata()
    get_mould_tabs_list()
    ElMessage.success('刷新成功！')
  })
  get_list() // 模板
  get_metadata() // 元数据
  get_mould_tabs_list() // 顶部栏 + 其下列表
  //   get_enterprise_list('') // 企业列表
  dictData('1547842801364897793').then((res) => {
    dict_list_1.value = res as any
  }) // 获取字典值 元数据 判断条件
})
onUnmounted(() => {
  EventBus.off('refresh')
})

const setup_submit = () => {
  // 将顶部栏以及元数据 配置到模板中
  var data = []
  for (let i in templateFormDealList.value) {
    if (templateFormDealList.value[i].is_checked) {
      var a = {
        formId: templateFormDealList.value[i].id,
        metadataDealList: [],
      }
      for (let ii in templateFormDealList.value[i]
        .templateFormMetadataDealList) {
        var a_1 = {
          metadataId:
            templateFormDealList.value[i].templateFormMetadataDealList[ii].id,
        }
        a.metadataDealList.push(a_1)
      }
      data.push(a)
    }
    for (let ii in templateFormDealList.value[i].children) {
      if (templateFormDealList.value[i].children[ii].is_checked) {
        var b = {
          formId: templateFormDealList.value[i].children[ii].id,
          metadataDealList: [],
        }
        for (let iii in templateFormDealList.value[i].children[ii]
          .templateFormMetadataDealList) {
          var b_1 = {
            metadataId:
              templateFormDealList.value[i].children[ii]
                .templateFormMetadataDealList[iii].id,
          }
          b.metadataDealList.push(b_1)
        }
        data.push(b)
      }
    }
  }
  console.log(data)
  axios
    .request({
      url: serviceApi.mould_template_editData + '/' + templateId.value,
      method: 'post',
      data: { formDealList: data },
    })
    .then((res) => {
      console.log(res)
      ElMessage.success('操作成功！')
      show_setUp_box.value = false
      get_mould_tabs_list()
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const show_setUp_box = ref(false) // 配置模板弹窗
const templateFormDealList = ref([]) // 配置参数列表
const templateFormDealList_index = ref(0) // 顶部索引
const selectChangeMetadataToTable = (e, index, index3) => {
  // 配置列表选择所需参数
  console.log(e, index, index3)
  templateFormDealList.value[index].children[
    index3
  ].templateFormMetadataDealList_1 = e
  templateFormDealList.value[index].children[
    index3
  ].templateFormMetadataDealList = []
  e.forEach((element) => {
    metadata.value.forEach((item) => {
      if (element == item.id) {
        templateFormDealList.value[index].children[
          index3
        ].templateFormMetadataDealList.push(item)
      }
    })
  })
  console.log(templateFormDealList.value)
}
const selectChangeMetadata = (e, index) => {
  // 配置顶部栏选择所需参数
  console.log(e, metadata.value)
  templateFormDealList.value[index].templateFormMetadataDealList_1 = e
  templateFormDealList.value[index].templateFormMetadataDealList = []
  e.forEach((element) => {
    metadata.value.forEach((item) => {
      if (element == item.id) {
        templateFormDealList.value[index].templateFormMetadataDealList.push(
          item
        )
      }
    })
  })
  console.log(templateFormDealList.value)
}
const remove_tab = (e) => {
  for (let i in mould_tabs.value) {
    if (mould_tabs.value[i].id == templateFormDealList.value[e].id) {
      mould_tabs.value[i].is_checked = false
      break
    }
  }
  templateFormDealList.value.splice(e, 1)
}
const change_setup_tabs = (e) => {
  console.log(e)
  var is_has = templateFormDealList.value.some((item, index) => {
    if (item.id == e.id) {
      templateFormDealList.value.splice(index, 1)
    }
    return item.id == e.id
  })
  if (!is_has) {
    e.templateFormMetadataDealList = []
    templateFormDealList.value.push(e)
  }
}
const templateId = ref()
const setUp_list = (e) => {
  // 获取模板配置 并回填到配置弹窗
  mould_tabs.value.forEach((element) => {
    element.is_checked = false
    element.templateFormMetadataDealList = []
    element.templateFormMetadataDealList_1 = []
    element.children.forEach((item) => {
      item.is_checked = false
      item.templateFormMetadataDealList = []
      item.templateFormMetadataDealList_1 = []
    })
  })
  templateFormDealList.value = []
  templateId.value = e.row.id
  templateFormDealList_index.value = 0
  axios
    .request({
      url: serviceApi.mould_template_getData + '/' + e.row.id,
      method: 'get',
    })
    .then((res) => {
      console.log(res)
      var list = res.data.formDealList ? res.data.formDealList : []
      var a = mould_tabs.value // 顶部栏原数据
      var b = metadata_select_list.value // 元数据  （参数）原数据
      for (let i in list) {
        var p_obj = [] // 选中tab中头部的参数
        for (let ii in list[i].metadataDealList) {
          for (let k in b) {
            if (b[k].id == list[i].metadataDealList[ii].metadataId) {
              p_obj.push(b[k])
            }
          }
        }
        for (let j in a) {
          if (a[j].id == list[i].formId) {
            // 如果该formId 为tab栏
            a[j].is_checked = true
            a[j].templateFormMetadataDealList = p_obj
            a[j].templateFormMetadataDealList_1 = p_obj.map((item) => {
              // 选中tab中头部的参数  供el-select用
              return item.id
            })
            templateFormDealList.value.push(a[j]) // 选中的tab值
            continue // 如果该formId 为tab栏 到此停止本次循环
          }
        }
        var temp = templateFormDealList.value
        for (let l in temp) {
          for (let ll in temp[l].children) {
            if (temp[l].children[ll].id == list[i].formId) {
              // 如果该formId 为tab下的列表
              temp[l].children[ll].is_checked = true
              temp[l].children[ll].templateFormMetadataDealList = p_obj
              temp[l].children[ll].templateFormMetadataDealList_1 = p_obj.map(
                (item) => {
                  // 选中tab中 table 的参数  供el-select用
                  return item.id
                }
              )
            }
          }
        }
      }

      console.log(templateFormDealList.value)
      show_setUp_box.value = true
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const update_mould_tabs = (e) => {
  add_mould_tabs_form_data.value = {
    id: e.row.id,
    name: e.row.name,
    code: e.row.code,
    parentId: e.row.parentId, // 有值  则代表该项为tab栏下的列表 *****
    remark: e.row.remark,
    sort: e.row.sort,
  }
  add_mould_tabs_box.value = true
}
const submit_mould_tabs = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      axios
        .request({
          url:
            add_mould_tabs_form_data.value.id == ''
              ? serviceApi.mould_tabs_crud
              : serviceApi.mould_tabs_crud +
                '/' +
                add_mould_tabs_form_data.value.id,
          method: add_mould_tabs_form_data.value.id == '' ? 'post' : 'put',
          data: add_mould_tabs_form_data.value,
        })
        .then((res) => {
          console.log(res)
          ElMessage.success('操作成功！')
          setTimeout(() => {
            get_mould_tabs_list()
            resetForm(formEl)
          }, 1000)
        })
        .catch((error) => {
          ElMessage.error(error)
        })
    }
  })
}
const get_mould_tabs_list = () => {
  loading2.value = true
  axios
    .request({
      url: serviceApi.mould_tabs_list,
      method: 'post',
      data: { current: 1, pageSize: 100 },
    })
    .then((res) => {
      console.log(res)
      loading2.value = false
      res.data.content.forEach((element) => {
        element.parentId === null ? '-1' : ''
        element.is_checked = false
      })
      // 转 二级树
      mould_tabs.value = arrToTree(res.data.content.reverse())
    })
    .catch((error) => {
      ElMessage.error(error)
    })
}
const update_list = (e) => {
  var list = []
  if (e.row.enterpriseList) {
    e.row.enterpriseList.forEach((element) => {
      list.push(element.id)
    })
  }
  add_form_data.value = {
    isEnable: e.row.isEnable,
    id: e.row.id,
    code: e.row.code,
    name: e.row.name,
    remark: e.row.remark,
    sort: e.row.sort,
    parentId: e.row.parentId,
    isLimitEnterprise: e.row.isLimitEnterprise,
    enterpriseList: list,
    type: add_form_data.value.type,
  }
  show_box.value = true
}
const enterprise_list = computed(() => {
  return store.state.enterprise_list
})
const remoteMethod = (e) => {
  enterprise_list.value.forEach((item) => {
    if (!item.name.includes(e)) {
      item.disabled = true
    } else {
      item.disabled = false
    }
  })
}
const submit_edit = () => {
  axios
    .request({
      url:
        delete_box_status.value == 2
          ? serviceApi.mould_disable + '/' + delete_id.value
          : serviceApi.mould_enable + '/' + delete_id.value,
      method: 'put',
    })
    .then((res) => {
      console.log(res)
      ElMessage.success('操作成功！')
      show_delete_box.value = false
      get_list()
    })
    .catch((error) => {
      ElMessage.error(error)
      show_delete_box.value = false
    })
}
const show_delete_box = ref(false)
const delete_box_status = ref()
const delete_id = ref()
const delete_name = ref()
const delete_tips = ref() // 删除标识 1 模板类型  2  元数据 3  顶部栏/表格
const delete_list = (e) => {
  console.log(e, list.value)
  var flag = false
  if (e.type == 2) {
    // 禁用父类型判断
    list.value.forEach((item) => {
      if (item.code == e.item.code) {
        flag = item.children.some((item1) => {
          return item1.isEnable == true
        })
      }
    })
  }
  if (flag) {
    ElMessage.error('存在启用状态的子类型，该类型不可禁用')
    return
  }
  delete_box_status.value = e.type
  show_delete_box.value = true
  delete_id.value = e.item.id
  delete_name.value = e.item.name
  delete_tips.value = e.delete_tips
}
const submit_delete = () => {
  axios
    .request({
      url:
        delete_tips.value == 1
          ? serviceApi.mould_crud + '/' + delete_id.value
          : delete_tips.value == 2
          ? serviceApi.metadata_crud + '/' + delete_id.value
          : serviceApi.mould_tabs_crud + '/' + delete_id.value,
      method: 'delete',
    })
    .then((res) => {
      console.log(res)
      ElMessage.success('删除成功！')
      show_delete_box.value = false
      delete_tips.value == 1
        ? get_list()
        : delete_tips.value == 2
        ? get_metadata()
        : get_mould_tabs_list()
    })
    .catch((err) => {
      ElMessage.error(err)
    })
}
const update_metadata = (e) => {
  console.log(e)
  metadata_form_data.value = {
    id: e.row.id,
    code: e.row.code,
    name: e.row.name,
    remark: e.row.remark,
    sort: e.row.sort,
    type: e.row.type.toString(),
    isCombination: e.row.isCombination,
    isTem: e.row.isTem,
    isAttribute: e.row.isAttribute,
    isRule: e.row.isRule,
    judgmentType: e.row.judgmentType ? e.row.judgmentType : 1,
    metadataValueDealList: e.row.judgmentType ? e.row.judgmentType : [],
    isPartsAttribute: e.row.isPartsAttribute,
    isPartsRule: e.row.isPartsRule,
    isMyPartsAttribute: e.row.isMyPartsAttribute,
    isMyPartsRule: e.row.isMyPartsRule,
  }
  if (e.row.isCombination) {
    // 如果组合类型 拆分code 得到对应的  组合规则  选择的元数据  以及数量
    combination_value.value = e.row.code.split('[')[0]
    let select_list = e.row.code.split('[')[1].split(']')[0].split(',')
    select_list.forEach((item) => {
      if (item.split('*').length == 2) {
        select_metadata_code_list_num.value.push({ num: item.split('*')[0] })
      } else {
        select_metadata_code_list_num.value.push({ num: '1' })
      }
      select_metadata_code_list.value.push(item.split('(')[1].split(')')[0])
    })
    console.log(select_list)
  }
  type_list_option.value = JSON.parse(JSON.stringify(e.row.metadataValueList))
  add_metadata_box.value = true
}
const submit_metadata = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      if (
        metadata_form_data.value.type == '4' &&
        type_list_option.value.length == 0
      ) {
        ElMessage.error('类型为下拉单选时，至少确定一项可选值')
        return
      }
      type_list_option.value.forEach((item, index) => {
        item.sort = index + 1
      })
      var data = Object.assign(metadata_form_data.value, {
        metadataValueDealList: type_list_option.value,
      })
      axios
        .request({
          url:
            metadata_form_data.value.id == ''
              ? serviceApi.metadata_crud
              : serviceApi.metadata_crud + '/' + metadata_form_data.value.id,
          method: metadata_form_data.value.id == '' ? 'post' : 'put',
          data: data,
        })
        .then((res) => {
          console.log(res)
          ElMessage.success('操作成功！')
          setTimeout(() => {
            type_list_option.value = []
            resetForm(formEl)
            get_metadata()
          }, 1000)
          if (data.isTem) {
            metadata_pageData.value.type_value = 1
            setTimeout(() => {
              get_metadata()
            }, 200)
          }
        })
        .catch((error) => {
          ElMessage.error(error)
        })
    }
  })
}
const submit = async (formEl: FormInstance | undefined) => {
  await formEl.validate((valid) => {
    if (valid) {
      console.log(valid)
      if (add_form_data.value.enterpriseList.length) {
        add_form_data.value.isLimitEnterprise = true
      }
      axios
        .request({
          url:
            add_form_data.value.id == ''
              ? serviceApi.mould_crud
              : serviceApi.mould_crud + '/' + add_form_data.value.id,
          method: add_form_data.value.id == '' ? 'post' : 'put',
          data: add_form_data.value,
        })
        .then((res) => {
          console.log(res)
          ElMessage.success('操作成功！')
          setTimeout(() => {
            show_box.value = false
            get_list()
          }, 1000)
        })
        .catch((error) => {
          ElMessage.error(error)
        })
    }
  })
}
const get_list = () => {
  loading.value = true
  axios
    .request({
      url: serviceApi.mould_list,
      method: 'post',
      data: page_data.value,
    })
    .then((res) => {
      console.log(res)
      // 转 二级树
      list.value = arrToTree(res.data.content.reverse())
      total.value = parseInt(res.data.totalElements)
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
      ElMessage.error(error)
    })
}
const metadata_select_list = ref() // 组合类型下--下拉选择列表
const get_metadata = () => {
  loading1.value = true
  let data = JSON.parse(JSON.stringify(metadata_pageData.value))
  if (data.type_value == 1) {
    Object.assign(data, { isTem: true })
  } else if (data.type_value == 2) {
    Object.assign(data, { isAttribute: true })
  } else if (data.type_value == 3) {
    Object.assign(data, { isRule: true })
  } else if (data.type_value == 4) {
    Object.assign(data, { isPartsAttribute: true })
  } else if (data.type_value == 5) {
    Object.assign(data, { isPartsRule: true })
  } else if (data.type_value == 6) {
    Object.assign(data, { isMyPartsAttribute: true })
  } else if (data.type_value == 7) {
    Object.assign(data, { isMyPartsRule: true })
  }
  axios
    .request({
      url: serviceApi.metadata_list,
      method: 'post',
      data: data,
    })
    .then((res) => {
      console.log(res)
      res.data.content.sort((a, b) => {
        return a.sort - b.sort
      })
      metadata.value = res.data.content
      if (data.type_value == 0 && data.name == '') {
        metadata_select_list.value = res.data.content
      }
      console.log(metadata_select_list.value)
      metadata_total.value = parseInt(res.data.totalElements)
      loading1.value = false
    })
    .catch((error) => {
      loading1.value = false
      ElMessage.error(error)
    })
}
const search_key = () => {
  console.log(header_list.value)
  page_data.value = {
    current: 1,
    pageSize: 1000,
    code: header_list.value[0].value,
    name: header_list.value[1].value,
    sort: header_list.value[2].value,
    isLimitEnterprise: header_list.value[3].value,
    isEnable: header_list.value[4].value,
    remark: header_list.value[5].value,
    type: page_data.value.type,
  }
  get_list()
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  select_metadata_code_list.value = []
  select_metadata_code_list_num.value = []
  metadata_form_data.value = {
    id: '',
    name: '',
    code: '',
    sort: '',
    remark: '',
    type: '1',
    isCombination: false,
    isTem: true,
    isAttribute: false,
    isRule: false,
    isPartsAttribute: false,
    isPartsRule: false,
    isMyPartsAttribute: false,
    isMyPartsRule: false,
    judgmentType: 1,
    metadataValueDealList: [],
  }
  add_form_data.value = {
    isEnable: false,
    id: '',
    name: '',
    code: '',
    parentId: '',
    enterpriseList: [],
    isLimitEnterprise: false,
    sort: '',
    remark: '',
    type: add_form_data.value.type,
  }
  add_mould_tabs_form_data.value = {
    id: '',
    name: '',
    code: '',
    parentId: '',
    remark: '',
    sort: '',
  }
  add_mould_tabs_box.value = false
  show_box.value = false
  add_metadata_box.value = false
}
const arrToTree = (res_list) => {
  var list_1 = []
  for (let i in res_list) {
    res_list[i].parentId == null ? (res_list[i].parentId = -1) : ''
    if (res_list[i].parentId == -1) {
      res_list[i].children = []
      list_1.push(res_list[i])
    }
  }
  for (let i in res_list) {
    for (let ii in list_1) {
      if (list_1[ii].id == res_list[i].parentId) {
        list_1[ii].children.push(res_list[i])
        list_1[ii].children.sort(function (a, b) {
          return a.sort - b.sort
        })
      }
    }
  }
  return list_1
}
</script>

<style lang="scss" scoped>
.meta_tabs {
  display: flex;
  align-items: baseline;
  > div {
    border: 1px solid #ccc;
    height: 30px;
    line-height: 30px;
    padding: 0 15px;
    margin-right: 10px;
    color: #666;
  }
  .select {
    border-color: #3580eb;
    color: #3580eb;
  }
}
:deep(.el-table__placeholder) {
  display: none;
}
.setup_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  > div {
    width: 50%;
  }
}
</style>
